<?php
error_reporting(E_ALL);
ini_set("display_errors", true);


function editRide($uid, $zip, $description, $seats, $depart_time)
{
	$bumConn = getConnection();
	$ride = "Update RidesAvailable Set destination_zip='$zip', description='$description', seats_available='$seats',"
			. "departure_time='$depart_time', updated_at=NOW() WHERE uid='$uid'";
			
	mysql_query($bumConn,$ride);		
	mysql_close();
}

function listRides($citystate, $uid=null, $strDate=null, $flex=null )
{
	$bumConn = getConnection();
	if (isset($destCity))
	{
		$citystate = explode(',', $citystate);
		$dest_city = trim($citystate[0]);
		$dest_state = trim($citystate[1]);
		$ride = "SELECT * FROM RidesAvailable WHERE dest_city='$dest_city' AND dest_state='$dest_state'";
	}
	else if (isset($uid))
		$ride = "SELECT * FROM RidesAvailable WHERE uid='$uid'";
	else
		$ride = "SELECT * FROM RidesAvailable WHERE 1=1";

	if(isset($strDate))
	{
		if(!isset($flex))$flex = 'day';
		$ride	.= " AND departure_time BETWEEN '"
				. date( 'Y-m-d H:i:s', strtotime($strDate ." - 1 " . $flex) )
				. "' AND '"
				.  date( 'Y-m-d H:i:s', strtotime( $strDate ." + 1 " . $flex) )
				. "'";
	}
	//print_debug($ride);
	$q_ride = mysql_query($ride, $bumConn);	
	return $q_ride;
	
}

function listRidesSummary()
{
	$bumConn = getConnection();
		$ride = "	SELECT dest_city City, dest_state State
					FROM RidesAvailable 
					GROUP BY dest_city, dest_state";
	
	$q_ride = mysql_query($ride, $bumConn);	
	echo mysql_error($bumConn);
	return $q_ride;
	
}

function GetNameFromZip($zip)
{
	if(!is_numeric($zip)) return $zip;
	$conn = getConnection();
	$sql = "SELECT City, State FROM zip_codes WHERE zip='$zip'";
	$result = mysql_query($sql, $conn);	
	$rs = mysql_fetch_assoc($result);
	if(!isset($rs))
	{
		$retVal = 'Sorry we could not find your city';
	}else{
		$retVal = $rs['City'] . ', ' . $rs['State'];
	}
	mysql_close($conn);
	return $retVal;
}